
#include "DequePersistent.h"
#include <algorithm>
#include <iostream>

#ifndef ZERO
	#define ZERO 0
#endif


DequePersistent::DequePersistent()
	: atributte(""),
	size(0),
	newSortedAtributte("")
{

}

DequePersistent::DequePersistent(const DequePersistent& right)
{
	//if((this != &right))
	//{

		if((right.size!= 0))
		{
			atributte = right.atributte;
			size = right.size;
			dqPersistent = right.dqPersistent;
		}

		if((right.size== 0))
		{
			size = right.size;
		}
//	}
}

DequePersistent::~DequePersistent(){
	// Tendria que ver el tema de la memoria, pero se supone que cuando se carga aca la memoria fue tomada
	// asi que la libero
	if(this->dqPersistent.empty()==false)
	{	
		this->dqPersistent.clear();
	}	
	if(this->dqPersistent.empty()==true)
	{	
		this->size=0;
	}
}

void DequePersistent::clear()
{
	for(std::deque<Persistent*>::iterator it = dqPersistent.begin(); it != dqPersistent.end(); it++)
	{
		if(*it != NULL)
		{	delete (*it);
			(*it)=NULL;
		}	
	//delete (*it);
	//(*it)=NULL;

	}
	if(this->dqPersistent.empty()==false)
	{	
		this->dqPersistent.clear();
	}
	if(this->dqPersistent.empty()==true)
	{	
		this->size=0;
	}

}



std::string DequePersistent::getAtributte(){
	return this->atributte;	
}

void DequePersistent::setAtributte(std::string at){
	this->atributte = at;
}

void DequePersistent::insertSort(Persistent* object){
	bool flag = false;
	std::deque<Persistent*>::iterator current = dqPersistent.begin();

	if(object != NULL)
	{		
		while(current != dqPersistent.end() && !flag)
		{
			if((*current)->EsMayor(this->atributte, object))
			{
				dqPersistent.insert(current, object);
				flag = true;
			}
			current++;
		}
		if(!flag)
		{
			dqPersistent.push_back(object);
		}
		this->size++;
	}	
}

int DequePersistent::getSize(){
	return dqPersistent.size();
}
Persistent* DequePersistent::getPersistent(int i)
{
	if (i < this->size)
	{
		
		return dqPersistent[i];
	}
	return NULL;
}

Persistent* DequePersistent::getFirst(){
	std::deque<Persistent*>::iterator it;
	
	if(this->dqPersistent.size() == ZERO)
		return NULL;
	it = this->dqPersistent.begin();
	return (*it);
}

Persistent* DequePersistent::getLast(){
	std::deque<Persistent*>::iterator it;
		
	if(this->dqPersistent.size() == ZERO)
		return NULL;
	it = this->dqPersistent.begin() + (this->dqPersistent.size()-1);
	return (*it);
}
void DequePersistent::popFront()
{
	if(dqPersistent.size() != 0)
	{
		if( dqPersistent[0] != NULL)
		{
			delete dqPersistent[0];
			dqPersistent[0] = NULL;
		}
		
		dqPersistent.pop_front();
		size = dqPersistent.size();
	}	
}

void DequePersistent::popBack()
{
	if(dqPersistent.size() != 0)
	{
		if( dqPersistent[dqPersistent.size() - 1] != NULL)
		{
			delete dqPersistent[dqPersistent.size() - 1];
			dqPersistent[dqPersistent.size() - 1] = NULL;
		}
		dqPersistent.pop_back();
		size = dqPersistent.size();
	}
}

bool DequePersistent::is(Persistent* object){
	int i = ZERO;
	
	for(i = ZERO; i < this->getSize(); i++)
	{
		if(this->getPersistent(i)->getKey() == object->getKey())
			return true;
	}
	return false;
}

int DequePersistent::indexOf(Persistent* object){
	bool find = false;
	int i = ZERO, retorno = -1;
	
	for(i = ZERO; i < this->getSize() && !find; i++)
	{
		if(this->getPersistent(i)->getKey() == object->getKey())
		{
			find = true;
			retorno = i;
		}
	}
	return retorno;
}

bool DequePersistent::Compare(Persistent *left, Persistent *right)
{
	std::string leftValue = left->getValor(this->newSortedAtributte);
	std::string rightValue = right->getValor(this->newSortedAtributte);
	return (leftValue < rightValue);
}

bool DequePersistent::operator()(Persistent *left, Persistent *right)
{
	std::string leftValue = left->getValor(this->newSortedAtributte);
	std::string rightValue = right->getValor(this->newSortedAtributte);
	return (leftValue < rightValue);
}

void DequePersistent::sort(std::string newAtributte)
{
	this->newSortedAtributte = newAtributte;
		
	std::sort(this->dqPersistent.begin(), this->dqPersistent.end(), (*this));
}

const DequePersistent & DequePersistent::operator=(const DequePersistent &right)
{
	if((this == &right))
	{
		return (*this);
	}
	
	if((right.size!= 0))
	{
		atributte = right.atributte;
		size = right.size;
		dqPersistent = right.dqPersistent;
		return (*this);
	}

	if((right.size== 0))
	{
		size = right.size;
		return (*this);
	}	
	return (*this);
}
//int DequePersistent::erase(int i)
//{	
//	if(i < this->size)
//	{
//		if((this->dqPersistent[i]) != NULL)
//		{
//			delete (this->lsPersistent[i]);
//			this->lsPersistent[i]=NULL;
//		}
//		lsPersistent.erase(lsPersistent.begin()+i);
//		size = lsPersistent.size();
//		return size;
//	}
//	return size;
//}

